package cn.com.bluemoon.daps.model.core.file.pgsql;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.validation.illegal.IllegalChar;
import cn.com.bluemoon.daps.model.core.file.TableData;
import cn.com.bluemoon.daps.model.core.file.excel.converter.MyWdTableCategoryIntegerStringConverter;
import cn.com.bluemoon.daps.model.core.file.validator.MyTopicCategory;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * <pre>
 *
 * 序号	表英文名	表中文名	表业务描述	标签	组路径	主题
 *
 * </pre>
 *
 * @author Jarod.Kong
 * @date 2021/2/5 12:01
 */
@Data
@ColumnWidth(20)
@HeadRowHeight(15)
@ContentRowHeight(20)
public class PgSqlTableData extends TableData {

    // common props
    @ExcelProperty("序号")
    @ColumnWidth(8)
    private Integer order;

    //    @NoSqlKeyword(dbtype = DbType.postgresql, name = "表英文名")
    @IllegalChar(message = "表英文名存在非法字符")
    @ExcelProperty("表英文名")
    @Size(max = 64, message = "表英文名不可以大于{max}")
    @NotNull(message = "表英文名不可为空")
    private String tableName;

    @ExcelProperty("表中文名")
    @Size(max = 2048, message = "表中文名不可以大于{max}")
    @NotNull(message = "表中文名不可为空")
    private String tableNameZh;

    @ExcelProperty("Schema")
    @Size(max = 32, message = "Schema不可以大于{max}")
    private String schema;

    @ExcelProperty("表业务描述")
    @Size(max = 1024, message = "业务描述不可以大于{max}")
    private String bizDesc;

    // @MyTableGroup
    @ExcelProperty(value = "组路径")
    @NotNull(message = "组路径不可为空")
    private String groupPathName;

    // pgsql props
    @MyTopicCategory
    @ExcelProperty("主题")
    @NotNull(message = "主题不可为空")
    private String topicName;

    /**
     * 1 表示维表，2 为事实表，3 贴源表
     */
    @ExcelProperty(value = "表分类", converter = MyWdTableCategoryIntegerStringConverter.class)
    @NotNull(message = "表分类不可为空")
    private Integer tableCategory;

    /**
     * 用于数据库表模型转为导出模型
     *
     * @param table 据库表模型
     */
    @Override
    public PgSqlTableData copyDbProperties0(DapDataModelTable table) {
        super.copyDbProperties0(table);
        setTableName(table.getEngName());
        setTableNameZh(table.getName());
        setBizDesc(table.getBusinessDesc());
        setSchema(table.getTableSchema());
        setTableCategory(table.getTableCategory());
        return this;
    }

    @Override
    protected ResultBean<DapDataModelTable> convertToModel0(DapDataModelTable modelTable) {
        modelTable.setTableSchema(getSchema());
        modelTable.setTableCategory(getTableCategory());
        return ResultBean.ok(modelTable);
    }

    public static PgSqlTableData getInstance(){
        return new PgSqlTableData();
    }

}
